<?php

namespace Kollway\CodeGenerator\Generators;

class ListDialogGenerator extends Generator
{
    public function generateDialogString()
    {
        $search_string = $this->getSearchString();
        $table_header = $this->getTableHeader();
        $table_body = $this->getTableBody();
        $pagination_string = $this->getPaginationString();
        $script_string = $this->getScriptString();

        $string = <<<HTML
@extends('public.modal_base')

@section('main-content')

  <style>
    td .entypo-icon:hover {
      cursor: pointer;
    }
  </style>

$search_string

  <div class="row">
    <div class="col-md-12">
      <table class="table table-bordered table-striped">
$table_header

$table_body
      </table>
    </div>
  </div>

$pagination_string
@stop

$script_string
HTML;
        $this->content = $string;

        return $string;
    }

    // 搜索部分
    protected function getSearchString()
    {
        $string = '';
        if ($this->search_fields) {
            $string .= <<<HTML
  <div class="row">
    <form id="search_form" class="search-form">
      @include('components.search', ['url'=>\Route::currentRouteName(), 'placeholder'=>'{$this->search_placeholder}'])
    </form>
  </div>
HTML;
        }

        return $string;
    }

    protected function getTableHeader()
    {
        $string = <<<HTML
        <thead>
        <tr>\n
HTML;
        $string .= <<<HTML
          <th>ID</th>\n
HTML;
        // 表头
        foreach ($this->column_info as $column) {
            if (!in_array($column['field'], ['id', 'status', 'sort_index', 'is_deleted', 'create_time', 'update_time'])) {
                $string .= <<<LIST
          <th>{$column['comment']}</th>\n
LIST;
            }
        }

        $string .= <<<HTML
          <th width="180">操作</th>
        </tr>
        </thead>
HTML;

        return $string;
    }

    protected function getTableBody()
    {
        $string = <<<HTML
        <tbody>

        @forelse(\$list as \$key=>\$val)
          <tr>
            <td>{{\$val['id']}}</td>\n
HTML;
        // 单元格
        foreach ($this->column_info as $column) {
            if (!in_array($column['field'], ['id', 'status', 'sort_index', 'is_deleted', 'create_time', 'update_time'])) {
                $string .= <<<LIST
            <td>{{\$val['{$column['field']}']}}</td>\n
LIST;
            }
        }

        $string .= <<<HTML
            <td>
              <a class="entypo-icon" {{entypo_tooltip('选中')}} onclick="choose{$this->model_name}({{json_encode(\$val)}})">
                <i class="entypo-check"></i>
              </a>
            </td>
          </tr>
        @empty
          <tr>
            <td colspan="99" class="text-center"> aOh! 暂时还没有内容!</td>
          </tr>
        @endforelse

        </tbody>
HTML;

        return $string;
    }

    // 分页部分
    protected function getPaginationString()
    {
        $string = <<<HTML
  @if(count(\$list) != 0)
    <div class="dataTables_wrapper my_pagination">
      {!! \$list->page !!}
    </div>
  @endif
HTML;
        return $string;
    }

    // script 部分
    protected function getScriptString()
    {
        $string = <<<HTML
@section('script')
  <script>
      function choose{$this->model_name}(data) {
          window.parent['choose_{$this->table}_cb'](data);
          hideModalFrame();
      }
  </script>
@stop
HTML;
        return $string;
    }
}
